/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.service.impl;

import com.minierp.bean.UserBean;
import com.minierp.dao.BusinessBillItemDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.OutputDao;
import com.minierp.err.NotAllowException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.BusinessBill;
import com.minierp.model.BusinessBillExt;
import com.minierp.model.BusinessBillItem;
import com.minierp.model.BusinessBillItemExt;
import com.minierp.model.Output;
import com.minierp.model.WorkProcess;
import com.minierp.service.OutputService;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OutputServiceImpl implements OutputService {
	public Map txInsert(long itemId, long processId, long makerId,
			String makerName, BigDecimal percent, Date makeDate, String mark,
			UserBean ub, List proList, List mtList, String comp,
			OutputDao outputDao, BusinessBillItemDao businessBillItemDao,
			IdGenerator idGenerator) throws Exception {
		BusinessBillItem bbItem = (BusinessBillItem) outputDao.get(
				BusinessBillItem.class, itemId);
		BusinessBillItemExt ie = (BusinessBillItemExt) outputDao.get(
				BusinessBillItemExt.class, itemId);
		if (bbItem == null) {
			throw new Exception("Business bill item not found for id " + itemId);
		} else {
			Output op = new Output();
			op.setBbItemId(bbItem.getId());
			op.setMakerId(makerId);
			op.setMakerName(makerName);
			op.setCreaterId(ub.getStaffer().getId());
			op.setCreaterName(ub.getStaffer().getStafferName());
			op.setId(idGenerator.getId(Output.class.getName()));
			op.setMakeDate(makeDate);
			op.setMark(mark);
			op.setPercent(percent);
			op.setProcessId(processId);
			outputDao.save(op);
			return this.doInsertExt(outputDao, businessBillItemDao, mtList,
					bbItem, ie, processId, proList, comp, ub);
		}
	}

	public Map txInsert(long itemId, long processId, List idArr, List nameArr,
			List perArr, Date makeDate, UserBean ub, List proList, List mtList,
			String comp, OutputDao outputDao,
			BusinessBillItemDao businessBillItemDao, IdGenerator idGenerator)
			throws Exception {
		BusinessBillItem bbItem = (BusinessBillItem) outputDao.get(
				BusinessBillItem.class, itemId);
		BusinessBillItemExt ie = (BusinessBillItemExt) outputDao.get(
				BusinessBillItemExt.class, itemId);
		if (bbItem == null) {
			throw new Exception("Business bill item not found for id " + itemId);
		} else {
			BigDecimal zero = new BigDecimal(0);
			int newNum = 0;

			for (int curId = 0; curId < idArr.size(); ++curId) {
				BigDecimal percent = new BigDecimal(perArr.get(curId)
						.toString());
				if (percent.compareTo(zero) > 0) {
					++newNum;
				}
			}

			long arg24 = 0L;
			if (newNum > 0) {
				arg24 = idGenerator.getId(Output.class.getName(), newNum);
			}

			for (int i = 0; i < idArr.size(); ++i) {
				BigDecimal percent1 = new BigDecimal(perArr.get(i).toString());
				if (percent1.compareTo(zero) > 0) {
					Output op = new Output();
					op.setBbItemId(bbItem.getId());
					op.setMakerId(Long.parseLong(idArr.get(i).toString()));
					op.setMakerName(nameArr.get(i).toString());
					op.setCreaterId(ub.getStaffer().getId());
					op.setCreaterName(ub.getStaffer().getStafferName());
					op.setId(arg24);
					op.setMakeDate(makeDate);
					op.setMark("");
					op.setPercent(percent1);
					op.setProcessId(processId);
					outputDao.save(op);
					++arg24;
				}
			}

			return this.doInsertExt(outputDao, businessBillItemDao, mtList,
					bbItem, ie, processId, proList, comp, ub);
		}
	}

	private Map doInsertExt(OutputDao outputDao,
			BusinessBillItemDao businessBillItemDao, List mtList,
			BusinessBillItem bbItem, BusinessBillItemExt ie, long processId,
			List proList, String comp, UserBean ub) throws Exception {
		HashMap map = new HashMap();
		List list = outputDao.getList(bbItem.getId());
		BigDecimal total = new BigDecimal(0);

		for (int completeStr = 0; completeStr < list.size(); ++completeStr) {
			Object[] nextPro = (Object[]) list.get(completeStr);
			if (processId == Long.parseLong(nextPro[0].toString())) {
				total = total.add((BigDecimal) nextPro[2]);
			}
		}

		if (total.compareTo(new BigDecimal(1)) > 0) {
			throw new NotAllowException("项目的每个工序的产量占比总和不能超过1");
		} else {
			String arg25 = this.getCompleteStr(list, proList);
			ie.setCompStr(arg25);
			WorkProcess arg26 = null;
			String currentProName = "";
			boolean compChange = false;
			boolean proChange = false;
			if (comp != null && ie.getWcComp().equals("N")) {
				if (comp.equals("PY")) {
					boolean itemList = false;

					for (int completed = 0; completed < proList.size(); ++completed) {
						WorkProcess bb = (WorkProcess) proList.get(completed);
						if (bb.getWorkCenterId() == bbItem.getWcId()) {
							if (itemList && bb.getEnabled().equals("Y")) {
								arg26 = bb;
								break;
							}

							if (bb.getId() == processId) {
								itemList = true;
								currentProName = bb.getProcessName();
							}
						}
					}

					if (arg26 != null) {
						ie.setProId(arg26.getId());
						ie.setProFlag("N");
						proChange = true;
					} else {
						Date arg28 = new Date();
						ie.setWcComp("Y");
						ie.setOperTime(arg28);
						ie.setOperId(ub.getStaffer().getId());
						ie.setOperName(ub.getStaffer().getStafferName());
						if (ie.getMaked().equals("N")) {
							ie.setMaked("Y");
							ie.setMakerId(ie.getOperId());
							ie.setMakerName(ie.getOperName());
							ie.setMakeTime(ie.getOperTime());
							ie.setProId(processId);
							ie.setProFlag("N");
						}

						compChange = true;
					}
				} else if (comp.equals("IY")) {
					ie.setWcComp("Y");
					ie.setOperTime(new Date());
					ie.setOperId(ub.getStaffer().getId());
					ie.setOperName(ub.getStaffer().getStafferName());
					if (ie.getMaked().equals("N")) {
						ie.setMaked("Y");
						ie.setMakerId(ie.getOperId());
						ie.setMakerName(ie.getOperName());
						ie.setMakeTime(ie.getOperTime());
						ie.setProId(processId);
						ie.setProFlag("N");
					}

					compChange = true;
				}
			}

			outputDao.update(ie);
			List arg27 = null;
			String arg29 = "Y";
			if (compChange) {
				BusinessBillExt arg30 = (BusinessBillExt) outputDao.get(
						BusinessBillExt.class, bbItem.getBillId());
				arg27 = businessBillItemDao.getListWidthExt(bbItem.getBillId());

				for (int msgContent = 0; msgContent < arg27.size(); ++msgContent) {
					Object[] i = (Object[]) arg27.get(msgContent);
					BusinessBillItem objs = (BusinessBillItem) i[0];
					BusinessBillItemExt item = (BusinessBillItemExt) i[1];
					if (objs.getWcId() != 0L && item.getWcComp().equals("N")
							|| objs.getIsCoop().equals("Y")
							&& item.getCoopComp().equals("N")) {
						arg29 = "N";
						break;
					}
				}

				arg30.setCompleted(arg29);
				if (arg30.getCompleted().equals("N")) {
					arg30.setCompleteTime((Date) null);
				} else {
					arg30.setCompleteTime(new Date());
				}

				outputDao.update(arg30);
			}

			BusinessBill arg31 = (BusinessBill) outputDao.get(
					BusinessBill.class, bbItem.getBillId());
			map.put("billId", Long.valueOf(arg31.getId()));
			map.put("billCode", arg31.getBillCode());
			map.put("estId", Long.valueOf(arg31.getClientId()));
			if (proChange && arg26.getDepId() != 0L) {
				map.put("proMsg", "Y");
				map.put("depId", Long.valueOf(arg26.getDepId()));
				map.put("mtId", Long.valueOf(bbItem.getMakeTypeId()));
				map.put("currProName", currentProName);
				map.put("msgContent",
						PageHelper.createMsgContent(bbItem, mtList));
			}

			if (compChange && arg29.equals("Y")) {
				String arg32 = "";

				for (int arg33 = 0; arg33 < arg27.size(); ++arg33) {
					Object[] arg34 = (Object[]) arg27.get(arg33);
					BusinessBillItem arg35 = (BusinessBillItem) arg34[0];
					if (!arg32.equals("")) {
						arg32 = arg32 + "<br>";
					}

					arg32 = arg32 + PageHelper.createMsgContent(arg35, mtList);
				}

				map.put("comp", "Y");
				map.put("msgContent", arg32);
				map.put("follIdStr", arg31.getFollowerIdStr());
				map.put("follNameStr", arg31.getFollowerNameStr());
				map.put("ordeDate", arg31.getOrderDate());
				map.put("deliDate", arg31.getDeliverDate());
				map.put("phone", arg31.getPhone());
				map.put("linkman", arg31.getLinkman());
				map.put("content", arg31.getContent());
			}

			return map;
		}
	}

	public String txDelete(long id, List proList, OutputDao outputDao,
			UserBean ub) throws Exception {
		String msg = "";
		Output op = (Output) outputDao.get(Output.class, id);
		if (op != null) {
			BusinessBillItem bbItem = (BusinessBillItem) outputDao.get(
					BusinessBillItem.class, op.getBbItemId());
			BusinessBillItemExt ie = (BusinessBillItemExt) outputDao.get(
					BusinessBillItemExt.class, op.getBbItemId());
			if (bbItem == null) {
				throw new Exception("Business bill item not found for id "
						+ op.getBbItemId());
			}

			BusinessBillExt be = (BusinessBillExt) outputDao.get(
					BusinessBillExt.class, bbItem.getBillId());
			if (ub.getUser().getUserType() != 1
					&& be.getCompleted().equals("Y")) {
				msg = "业务单已经完工，只有admin账号才能删除产量登记。";
			} else {
				outputDao.delete(id);
				List list = outputDao.getList(bbItem.getId());
				String completeStr = this.getCompleteStr(list, proList);
				ie.setCompStr(completeStr);
				outputDao.update(ie);
				msg = "删除成功！";
			}
		}

		return msg;
	}

	private String getCompleteStr(List list, List proList) {
		String completeStr = "";
		long preId = 0L;

		for (int j = 0; j < list.size(); ++j) {
			Object[] objs = (Object[]) list.get(j);
			long processId = Long.parseLong(objs[0].toString());
			if (preId == processId) {
				if (!completeStr.equals("")) {
					completeStr = completeStr + " " + objs[1] + " " + objs[2];
				} else {
					completeStr = EncCommonHelper.getSimpleObjectName(proList,
							processId) + "：" + objs[1] + " " + objs[2];
				}
			} else {
				preId = processId;
				if (!completeStr.equals("")) {
					completeStr = completeStr + "；\n";
				}

				completeStr = completeStr
						+ EncCommonHelper.getSimpleObjectName(proList,
								processId) + "：" + objs[1] + " " + objs[2];
			}
		}

		if (completeStr.length() > 100) {
			completeStr = completeStr.substring(0, 97) + "...";
		}

		return completeStr;
	}
}